#include "EPSG/Material.h"

/* System Includes */
#ifdef WIN32
	#include <Windows.h>
#endif

/* OpenGL Includes */
 #include <GL/GL.h>

EPSG::Material* EPSG::Material::_whiteMaterial = NULL;

EPSG::Material::Material()
{
	_ambient[0] = 1;
	_ambient[1] = 1;
	_ambient[2] = 1;
	_ambient[3] = 1;
	_diffuse[0] = 0;
	_diffuse[1] = 0;
	_diffuse[2] = 0;
	_diffuse[3] = 0;
	_specular[0] = 0;
	_specular[1] = 0;
	_specular[2] = 0;
	_specular[3] = 0;

	_shininess = 0.0f;
}

bool EPSG::Material::load()
{
	glPushAttrib( GL_LIGHTING_BIT );

	glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, _ambient );
	glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, _diffuse );
	glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, _specular );
	glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, _shininess );
	return false;
}

void EPSG::Material::unload()
{
	glPopAttrib();
}

void EPSG::Material::setAmbient( float r, float g, float b, float a )
{
	_ambient[0] = r;
	_ambient[1] = g;
	_ambient[2] = b;
	_ambient[3] = a;
}

void EPSG::Material::setDiffuse( float r, float g, float b, float a )
{
	_diffuse[0] = r;
	_diffuse[1] = g;
	_diffuse[2] = b;
	_diffuse[3] = a;
}

void EPSG::Material::setSpecular( float r, float g, float b, float a )
{
	_specular[0] = r;
	_specular[1] = g;
	_specular[2] = b;
	_specular[3] = a;
}

void EPSG::Material::setShininess( float shininess )
{
	_shininess = shininess;
}

EPSG::Material* EPSG::Material::whiteMaterial()
{
	if( _whiteMaterial == NULL )
	{
		_whiteMaterial = new EPSG::Material();
		_whiteMaterial->setAmbient( 0.3f, 0.3f, 0.3f, 1 );
		_whiteMaterial->setDiffuse( 0.8f, 0.8f, 0.8f, 1 );
		_whiteMaterial->setSpecular( 0.1f, 0.1f, 0.1f, 1 );
		_whiteMaterial->setShininess( 0 );
	}

	return _whiteMaterial;
}